<?php

class Catalogue_Model_Attribute extends Catalogue_Model_Object_Abstract implements Workset_Model_Value_Form_Interface {

    const FLAG_VALUE_FILE = 1;
    const FLAG_VALUE_MULTIPLE = 2;
    const FLAG_VALUE_ENUM = 4;
    const FLAG_VALUE_INT = 8;
    const FLAG_VALUE_STRING = 16;
    const FLAG_VALUE_BOOL = 32;
    const FLAG_VALUE_DECIMAL = 64;
    const FLAG_VALUE_TEXT = 128;
    const FLAG_VALUE_IMAGE = 256;
    const FLAG_VALUE_VIDEO = 512;

    /**
     * Тип объекта
     * @var string
     */
    protected $_objectType = Catalogue_Model_Object_Abstract::T_ATTRIBUTE;

    protected $_defaultValue = null;

    protected $_value = null;

	static $_fileTypes = array(self::T_FILE, self::T_IMAGE, self::T_VIDEO);

	static $_types = array(
        self::T_INT, self::T_STRING, self::T_BOOL, self::T_DECIMAL, self::T_TEXT, self::T_SELECT, 
        self::T_FILE, self::T_IMAGE, self::T_VIDEO
    );

    static $_binTypes = array(
        self::T_INT         => self::FLAG_VALUE_INT,
        self::T_STRING      => self::FLAG_VALUE_STRING,
        self::T_BOOL        => self::FLAG_VALUE_BOOL,
        self::T_DECIMAL     => self::FLAG_VALUE_DECIMAL,
        self::T_TEXT        => self::FLAG_VALUE_TEXT,
        self::T_IMAGE       => self::FLAG_VALUE_IMAGE,
        self::T_VIDEO       => self::FLAG_VALUE_VIDEO,
    );

    static $_mimeTypes = array(
        self::T_IMAGE => array(
            'image/gif',
            'image/jpeg',
            'image/png',
            'image/bmp'
        )
	);

    static $_fileinfos = array(
        self::T_IMAGE       => 'Workset_File_Info_Image',
        self::T_FILE        => 'Workset_File_Info',
        self::T_VIDEO       => 'Workset_File_Info_Video'
    );

	static function getInstance($identity) {
		$storage = self::getDefaultStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE);
		return $storage->getOne($identity);
	}

	static function create($data) {
		$storage = self::getDefaultStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE);
		return $storage->create($data);
	}

    static function getAll($where = null, $order = null, $limit = null, $offset = null, $offsetByPage = false) {
        $storage = self::getDefaultStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE);
        return $storage->getAll($where, $order, $limit, $offset, $offsetByPage);
    }

    static function getByCode($code) {
        $storage = self::getDefaultStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE);
        return $storage->getAll(array('code = ?' => $code));
    }

    static function getAvailableMimeTypes($category) {
        if (isset(self::$_mimeTypes[$category])) {
            return self::$_mimeTypes[$category];
        }
        return null;
    }

    static function getTypes($format = 'array', $labels = array()) {
        $types = array(
            self::T_STRING,
            self::T_BOOL,
            self::T_INT,
            self::T_DECIMAL,
            self::T_TEXT,
            self::T_IMAGE,
            self::T_FILE,
            self::T_VIDEO
        );

        if ($format == 'array') {
            return $types;
        }

        if ($format == 'jqgrid') {
            $namedTypes = array();

            if (empty($labels)) {
                foreach ($types as $type) {
                    $namedTypes[] = $type.":".$type;
                }
            } else {
                foreach ($types as $type) {
                   if (isset($labels[$type])) {
                       $label = $labels[$type];
                   } else {
                       $label = $type;
                   }
                   $namedTypes[] = $type.':'.$label;
                }
            }
            
            return implode(';', $namedTypes);
        }
    }

	static function getRequiredParams($type = null) {
		if ($type == 'jqgrid') {
			$list = array(
				self::NOT_REQUIRED.':Необязательный',
				self::REQUIRED.':Обязательный',
				self::CONDITIONAL_REQUIRED.':Желательный',
			);
			return implode(';', $list);
		} else {
			return array(
				self::NOT_REQUIRED,
				self::REQUIRED,
				self::CONDITIONAL_REQUIRED
			);
		}

	}

    static function setFileinfoClass($class, $type) {
        if (in_array($type, self::$_types) && class_exists($class)) {
            self::$_fileinfos[$type] = $class;
        }
    }

    static function setFileMimetype($type, $mime) {
        if (isset(self::$_mimeTypes[$type])) {
            if (!in_array($mime, self::$_mimeTypes[$type])) {
                self::$_mimeTypes[$type][] = $mime;
            }
        }
    }

    function getFileinfoClass($type = null, $mime = null) {

        if (null !== $mime) {
            
            if (null !== $type) {
                $array = array(self::$_fileinfos[$type]);
            } else {
                $array = & self::$_fileinfos;
            }

            foreach ($array as $key => $group) {
                if (in_array($mime, $group)) {
                    return self::$_fileinfos[$key];
                }
            }
            
        } elseif (null !== $type && isset(self::$_fileinfos[$type])) {
            return self::$_fileinfos[$type];
        } elseif ($this->isImage()) {
            return self::$_fileinfos[self::T_IMAGE];
        } elseif ($this->isVideoFile()) {
            return self::$_fileinfos[self::T_VIDEO];
        } elseif ($this->isFile()) {
            return self::$_fileinfos[self::T_FILE];
        }

        return null;
    }

    function getDefaultValue() {
        return $this->_defaultValue;
    }

    function setDefaultValue($value) {
        if (is_array($value) && isset($value['value'])) {
            $this->_defaultValue = $value['value'];
        } else {
            $this->_defaultValue = $value;
        }
    }

	function init() {
		$this->attach(new Catalogue_Model_Attribute_Observer_ValueTypeUpdater());
	}

    function setMultiple($flag) {
        $this->_data['multiple'] = (boolean)$flag;
    }

    function getValueFlagList() {
        $flags = array();

        if ($this->isMultiple()) {
            $flags[] = self::FLAG_VALUE_MULTIPLE;
        }

        if ($this->isFile()) {
            $flags[] = self::FLAG_VALUE_FILE;
        }

        if ($this->isEnum()) {
            $flags[] = self::FLAG_VALUE_ENUM;
        }

        $t = $this->getType();

        if (isset(self::$_binTypes[$t])) {
            $flags[] = self::$_binTypes[$t];
        }

        return $flags;
    }

    function setType($type) {
        $type = strtolower($type);
        if (in_array($type, self::$_types)) {
            $this->_data['type'] = $type;
        }
    }

    function getName() {
        return $this->_data['name'];
    }

    function isRequired($level = null) {
        if (null === $level) {
            return $this->_data['mandatory'] > 0;
        } else {
            return $this->_data['mandatory'] == $level;
        }
    }

    function isEnum() {
        return $this->_data['enum_value'] > 0;
    }

    function getEnumItems() {
        if ($this->isEnum()) {
            $enum = new Catalogue_Model_Attribute_Enum($this);
            return $enum->getItems();
        }
    }

    function getModelId() {
        return $this->getId();
    }

    function isMultiple() {
        return $this->_data['multiple'] > 0;
    }

    function getValue() {
        if (null !== $this->_value) {
            return $this->_value;
        } else {
            return $this->getDefaultValue();
        }
    }

    function isSelect() {
        return $this->_data['type'] == self::T_SELECT || $this->isEnum();
    }

    function render($content = null) {
        return $this->_value;
    }

    function getHtmlId($prefix = null, $postfix = null) {
        return parent::getHtmlId($prefix, $postfix);
    }

	function getType() {
		return !empty($this->_data['type']) ? $this->_data['type'] : self::T_STRING;
	}

    function typeOf($type) {

        $t = $this->getType();

        if ($type == $t) {
            return true;
            
        } elseif (!$t && $type == self::T_STRING) {
            return true;
        }
        
        $success = false;

        switch ($type) {
            case self::T_FILE:
                $success = $this->isFile();
            break;

            case self::T_IMAGE:
                $success = $this->isImage();
            break;

            case self::T_VIDEO:
                $success = $this->isVideo();
            break;
        }

        return $success;
    }

    function isFile() {
        return in_array($this->_data['type'], self::$_fileTypes, true);
    }

    function isImage() {
        return $this->_data['type'] == self::T_IMAGE;
    }

	function isVideo() {
		return $this->_data['type'] == self::T_VIDEO;
	}

    function isInt() {
        return $this->_data['type'] == self::T_INT;
    }

    function isDecimal() {
        return $this->_data['type'] == self::T_DECIMAL;
    }

    function isString() {
        return $this->_data['type'] == self::T_STRING || !$this->_data['type'];
    }

    function isBool() {
        return $this->_data['type'] == self::T_BOOL;
    }

    function isText() {
        return $this->_data['type'] == self::T_TEXT;
    }
    
    function __toString() {
        return $this->render();
    }
    

}

?>